<template>
  <div class="container">
    <div class="search-box">
      <el-input
        placeholder="请输入内容"
        class="input-box"
        v-model="table"
      ></el-input>
      <el-button @click="qingchuAn">清除</el-button>
      <el-button type="primary" @click="sousuoAn">搜索</el-button>
    </div>
    <el-table border style="width: 100%" :data="list">
      <el-table-column type="index" label="任务编号" width="80">
      </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button
            size="mini"
            style="margin-right: 10px"
            @click="getputbianJi(row.id)"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="shanchuAn(row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params._page"
        :page-sizes="[5, 3, 5, 10]"
        :page-size="params._limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <el-button size="small" plain @click="xinzengAn">+ 新增</el-button>
      <el-dialog width="30%" :visible="showRoleDialog" @close="closeButton">
        <template>
          <!-- 新增弹层 -->
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="Form"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="活动名称" prop="name">
              <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="活动区域" prop="des">
              <el-input v-model="ruleForm.des"></el-input>
            </el-form-item>
          </el-form>
        </template>
        <template #footer>
          <el-button type="primary" size="small" @click="confirm"
            >确定</el-button
          >
          <el-button size="small" @click="confirmadd">取消</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getleiBiao,
  deleteshanChu,
  postxinZeng,
  putbianJi,
  getxiangQing,
  getshouSuo,
} from "@/api/index.js";
export default {
  data() {
    return {
      list: [],
      params: {
        _page: 1,
        _limit: 5,
      },
      total: 0,
      showRoleDialog: false,
      ruleForm: {
        name: "",
        des: "",
        id: "",
      },
      rules: {
        name: [{ required: true, message: "内容不能为空", trigger: ["blur"] }],
        des: [{ required: true, message: "内容不能为空", trigger: ["blur"] }],
      },
      table: "",
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    async getList() {
      const res = await getleiBiao(this.params);
      // console.log(res.data);
      this.list = res.data;
      this.total = Number(res.headers["x-total-count"]);
    },
    // 分页
    handleSizeChange(val) {
      this.params._limit = val;
      // this.params._page = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.params._page = val;
      this.getList();
    },
    async shanchuAn(id) {
      await deleteshanChu(id);
      this.$message.success("删除成功");
      this.getList();
    },
    xinzengAn() {
      this.showRoleDialog = true;
    },
    closeButton() {
      this.showRoleDialog = false;
      this.appForm();
    },
    //取消
    confirmadd() {
      this.showRoleDialog = false;
      this.appForm();
    },
    confirm() {
      this.$refs.Form.validate(async (valid) => {
        if (valid) {
          if (this.ruleForm.id) {
            await putbianJi(this.ruleForm);
          } else {
            await postxinZeng(this.ruleForm);
          }
          this.$message.success(`${this.ruleForm.id ? "编辑" : "新增"} 成功`);
          this.$refs.Form.resetFields(); // 清空列表数据
          this.showRoleDialog = false;
          this.appForm();
          this.getList();
        }
      });
    },
    appForm() {
      this.ruleForm = {
        name: "",
        des: "",
        id: "",
      };
    },
    async getputbianJi(id) {
      this.showRoleDialog = true;
      const add = await getxiangQing(id);
      this.ruleForm = add.data;
    },
    async sousuoAn() {
      const res = await getshouSuo(this.table);
      this.list = res.data;
    },
    qingchuAn() {
      this.table = "";
      this.getList();
    },
  },
};
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
